- 這段課程的主題是 R 繪圖,包含以下概念:
- 基本繪圖原理
ggplot2- 實際案例練習
November 18, 2016
ggplot2attach(mtcars)
plot(wt, mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
hist(mtcars$mpg)
# 用 par() 來設定繪圖參數 par() # 列出現有設定 opar <- par() # 複製現有設定 par(col.lab="red") # 把座標軸說明改成紅色 hist(mtcars$mpg) # 用新設定繪圖 par(opar) # 回復原有設定
par(mfrow=c(1,2)) # 把繪圖區分割成 1x2 (一列,兩欄) hist(mtcars$mpg) hist(mtcars$wt)
ggplot2ggplot2簡介
grammar of graphics (Wilkinson, 2005)ggplot2有些用途,用 ggplot2是不行的
grammar of graphicsggplot(data=mpg) + # 資料層 aes(x=displ, y=hwy, color=drv) + # 資料和圖形元件的映射 geom_point() # 用「點」畫這張圖
grammar of graphics將一張圖分割成各自獨立的元件,類似 XML 的概念,這些元件包括:
plot 函數 (或相似的函數) 初始化一張畫布text, lines, points, axis) 來編修畫布xyplot, bwplot, etc.)mpg: 1999 - 2008年 38 個車款的燃油效率資料
require(ggplot2) str(mpg)
## Classes 'tbl_df', 'tbl' and 'data.frame': 234 obs. of 11 variables: ## $ manufacturer: chr "audi" "audi" "audi" "audi" ... ## $ model : chr "a4" "a4" "a4" "a4" ... ## $ displ : num 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ... ## $ year : int 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ... ## $ cyl : int 4 4 4 4 6 6 6 4 4 4 ... ## $ trans : chr "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ... ## $ drv : chr "f" "f" "f" "f" ... ## $ cty : int 18 21 20 21 16 18 18 18 16 20 ... ## $ hwy : int 29 29 31 30 26 26 27 26 25 28 ... ## $ fl : chr "p" "p" "p" "p" ... ## $ class : chr "compact" "compact" "compact" "compact" ...
qplot(displ, hwy, data = mpg)
qplot(displ, hwy, data = mpg, color = drv)
ggplot(data=mpg) + # 資料層 aes(x=displ, y=hwy, color=drv) + # 資料和圖形元件的映射 geom_point() # 用「點」畫這張圖
qplot(displ, hwy, data = mpg, geom = c("point", "smooth"))
qplot(hwy, data = mpg, fill = drv)
qplot(displ, hwy, data = mpg, facets = . ~ drv)
qplot(hwy, data = mpg, facets = drv ~ ., binwidth = 2)
str(maacs)
## 'data.frame': 750 obs. of 5 variables: ## $ id : int 1 2 3 4 5 6 7 8 9 10 ... ## $ eno : num 141 124 126 164 99 68 41 50 12 30 ... ## $ duBedMusM: num 2423 2793 3055 775 1634 ... ## $ pm25 : num 15.6 34.4 39 33.2 27.1 ... ## $ mopos : Factor w/ 2 levels "no","yes": 2 2 2 2 2 2 2 2 2 2 ...
ggplot(data = maacs, aes(log(eno))) + geom_histogram()
# Alternative: # qplot(log(eno), data = maacs)
ggplot(data = maacs, aes(log(eno), fill=mopos)) + geom_histogram()
# Alternative: # qplot(log(eno), data = maacs, fill = mopos)
ggplot(data = maacs, aes(log(eno))) + geom_density()
# Alternative: # qplot(log(eno), data = maacs, geom = "density")
ggplot(data = maacs, aes(log(eno), color=mopos)) + geom_density()
# Alternative: # qplot(log(eno), data = maacs, geom = "density", color = mopos)
ggplot(data=maacs, aes(x=log(pm25), y=log(eno))) + geom_point()
#qplot(log(pm25), log(eno), data = maacs)
ggplot(data=maacs, aes(x=log(pm25), y=log(eno))) + geom_point(aes(shape=mopos))
#qplot(log(pm25), log(eno), data = maacs, shape = mopos)
ggplot(data=maacs, aes(x=log(pm25), y=log(eno))) + geom_point(aes(colour=mopos))
#qplot(log(pm25), log(eno), data = maacs, color = mopos)
ggplot(data=maacs, aes(log(pm25), log(eno))) + geom_point(aes(colour=mopos)) + geom_smooth()
#qplot(log(pm25), log(eno), data = maacs, color = mopos, geom = c("point", "smooth"))
ggplot(data=maacs, aes(log(pm25), log(eno))) + geom_point() + geom_smooth() + facet_grid(.~mopos)
#qplot(log(pm25), log(eno), data = maacs, geom = c("point", "smooth"),
# facets = . ~ mopos)
xlab(), ylab(), labs(), ggtitle()theme()
theme(legend.position = "none")theme_gray(): 預設主題 (灰底)theme_bw(): 黑白主題,較乾淨g <- ggplot(maacs, aes(log(pm25), log(eno))) g + geom_point(color = "steelblue", size = 4, alpha = 1/2)
g + geom_point(aes(color = mopos), size = 4, alpha = 1/2)
g + geom_point(aes(color = mopos)) + labs(title = "MAACS Cohort") +
labs(x = expression("log " * PM[2.5]), y = expression("log " * eNO))
g + geom_point(aes(color = mopos), size = 2, alpha = 1/2) +
geom_smooth(size = 4, linetype = 3, method = "lm", se = FALSE)
g + geom_point(aes(color = mopos)) + theme_bw(base_family = "Times")
testdat <- data.frame(x = 1:100, y = rnorm(100)) testdat[50,2] <- 100 ## Outlier! plot(testdat$x, testdat$y, type = "l", ylim = c(-3,3))
g <- ggplot(testdat, aes(x = x, y = y)) g + geom_line()
g + geom_line() + ylim(-3, 3)
g + geom_line() + coord_cartesian(ylim = c(-3, 3))